package control

import (
	"chat_project/args"
	"chat_project/service"
	"chat_project/util"
	"fmt"
	"net/http"
)

type ContactControl struct {
}

var contactService service.ContactService

// 添加好友
func (ContactControl) AddFriend(writer http.ResponseWriter, request *http.Request) {

	fmt.Println("进入control.AddFriend")
	// str -> int 前端传递的参数,需要进行格式处理,定义一个结构体,将参数处理后自动绑定到结构体中
	// 参数的对象化管理 todo 这里是核心提高代码
	var arg args.ContactArg
	// 处理参数类型和格式,类似于python中web框架的mashroom
	util.Bind(request, &arg)
	err := contactService.AddFriend(arg.Userid, arg.Dstid)
	if err != nil {
		returnJson.ReturnJsonStr(writer, -1, err.Error(), nil)
	} else {
		returnJson.ReturnJsonStr(writer, 0, "添加好友成功", nil)
	}

}

func (ContactControl) SearchFriend(writer http.ResponseWriter, request *http.Request) {
	fmt.Println("进入control.SearchFriend")
	var arg args.ContactArg
	// 处理参数类型和格式,类似于python中web框架的mashroom
	util.Bind(request, &arg)
	users := contactService.SearchFriend(arg.Userid)
	returnJson.ReturnJsonList(writer, 0, users, len(users))
}
